//
//  WHRCropManureVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/5.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "WHRCropManureVC.h"
#import "CropIntroLeftTableviewCell.h"
#import "CropIntroLeftTableviewModel.h"


@interface WHRCropManureVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray *tableViewDataArray;
@property (nonatomic , strong) UIImageView *img;

@end

@implementation WHRCropManureVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self creatView];
    [self setlayout];
    [self.view addSubview:self.tableView];
    [self requestSecondLevelList];
    
}

- (void)creatView {
    
    self.img = [[UIImageView alloc]init];
    self.img.contentMode =UIViewContentModeScaleAspectFill;
    self.img.clipsToBounds=YES;//
    [self.img sd_setImageWithURL:[NSURL URLWithString:_cropImg] placeholderImage:[UIImage imageNamed:@"noCropImg"]];
    [self.view addSubview:self.img];
    
    
    
}

- (void)setlayout {
    
    self.img.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(SCREEN_WIDTH)
    .heightIs(200) ;
    
}


- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, 80, SCREEN_HEIGHT)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight = 44;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorColor = [UIColor clearColor];
        [_tableView  registerClass:[CropIntroLeftTableviewCell class] forCellReuseIdentifier:[CropIntroLeftTableviewCell reuseID]];
        
    }
    return _tableView;
}


#pragma mark - Getters

- (NSMutableArray *)tableViewDataArray
{
    if (!_tableViewDataArray)
    {
        _tableViewDataArray = [NSMutableArray array];
    }
    return _tableViewDataArray;
}


#pragma mark - requestSecondLevelList

- (void)requestSecondLevelList {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"cropColumnBase"] = @"syq";
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle,  URL_AgricultureCircle_findColumnByBase) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.tableViewDataArray = [CropIntroLeftTableviewModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"columnList"]];
            
            [self.tableView reloadData];
            
            if (self.tableViewDataArray.count > 0) {
                
                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            }
            
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
        
        
    } failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
    
}


#pragma mark - UITableView DataSource Delegate



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CropIntroLeftTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:[ CropIntroLeftTableviewCell reuseID] forIndexPath:indexPath];
    CropIntroLeftTableviewModel *model = self.tableViewDataArray[indexPath.row];
    cell.name.text = model.columnName;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    _selectIndex = indexPath.row;
    //    /** 调用二级接口 */
    //    LeftTableViewModel *model = self.dataSource[indexPath.row];
    //    [self getCollectionData:model.classificationId];
    
}

@end
